//
//  MCChargeInDetailViewController.m
//  ERP
//
//  Created by QA on 15/11/5.
//  Copyright © 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MCChargeInDetailViewController.h"

#import "WebServiceCommonRequest.h"
#import "UITableView+Custom.h"
#import "NSDictionary+DataFormat.h"

#import "GEObjectValueTableViewCell.h"
#import "GEProgressBarTableViewCell.h"
#import "AutoTextTableViewCell.h"
#import "MCChargeInDetailViewCell.h"

#import "Charts-Swift.h"
#import "MJRefresh.h"
#import "MCChargeInWeighDetailViewController.h"
#import "StringAxisValueFormatter.h"

#import "GEObjectButton.h"
#import "YFViewPager.h"

@interface MCChargeInDetailViewController ()

@property (nonatomic,strong) NSDictionary *contractBaseInfo;
@property (nonatomic,strong) NSMutableArray *chargeInBaseData;
@property (strong, nonatomic) NSString *searchStartTime;
@property (strong, nonatomic) NSString *searchEndTime;

@property (nonatomic,strong) NSMutableArray *stateData;
@property (nonatomic,assign) NSInteger stateContentState;//0加载中，1未找到数据，2加载失败，3加载成功

@end

@implementation MCChargeInDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"原材进料统计明细"];
    }
    return self;
}

- (void)getPassingParameter:(id)object
{
    if(![object isKindOfClass:[NSArray class]]) return;
    NSArray *objectValue = object;
    
    self.searchStartTime = objectValue[0];
    self.searchEndTime = objectValue[1];
    NSDictionary *dic = objectValue[2];
    self.contractBaseInfo = dic;
    
    NSMutableArray *chargeInBase = [NSMutableArray array];
    [self addContractInfoKey:@"供应商" toKey:@"供应商名称：" defaults:@"未知" format:nil form:dic toArray:chargeInBase];
    [self addContractInfoKey:@"材料名称" toKey:@"材料名称：" defaults:@"未知" format:nil form:dic toArray:chargeInBase];
    [self addContractInfoKey:@"打印净重" toKey:@"材料进料量：" defaults:@"未知" format:@"%@吨" form:dic toArray:chargeInBase];
    self.chargeInBaseData = chargeInBase;
}

- (void)addContractInfoKey:(id)key toKey:(id)tokey defaults:(id)def format:(id)formatText
                      form:(NSDictionary*)dic toArray:(NSMutableArray*)array
{
    id value = [dic objectFormatForKey:key];
    if (!def) def = @"";
    
    if (!value){
        value = def;
    } else if (formatText) {
        value = [NSString stringWithFormat:formatText,value];
    }
    
    NSDictionary *valueDic = [NSDictionary dictionaryWithObject:value forKey:tokey];
    [array addObject:valueDic];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.stateData = [NSMutableArray array];
    
    //获取合同下近10单所有订单
    [self headerRereshing];
}

- (void)setupRefresh
{
    //添加下拉加载数据
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

- (void)stopRefresh
{
    //移除下拉加载数据
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 开始进入刷新状态

- (void)headerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:0];
    NSNumber *endNumber = [NSNumber numberWithInteger:10];
    [self getJobOrderInfoIsReresh:YES state:stateNumber end:endNumber];
}

- (void)footerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:self.stateData.count + 1];
    NSNumber *endNumber = [NSNumber numberWithInteger:self.stateData.count + 10];
    [self getJobOrderInfoIsReresh:NO state:stateNumber end:endNumber];
}

#pragma mark - 网络获取任务单
- (void)getJobOrderInfoIsReresh:(BOOL)isReresh state:(NSNumber*)state end:(NSNumber*)end
{
    //判断是否需要获取订单
    if(!self.contractBaseInfo) return;
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:@"YCL_JLTJ_DETAILED20160201"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    //设置站点ID
    StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
    [content setStationID:[model.stationId integerValue]];
    
    [content addParameterKey:@"起始条数" value:state];
    [content addParameterKey:@"结束条数" value:end];
    [content addParameterKey:@"CLID" value:[self.contractBaseInfo objectFormatStringForKey:@"CLID"]];
    [content addParameterKey:@"GYSID" value:[self.contractBaseInfo objectFormatStringForKey:@"GYSID"]];
    [content addParameterKey:@"开始日期" value:self.searchStartTime];
    [content addParameterKey:@"截止日期" value:self.searchEndTime];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(isReresh) [self.tableView headerEndRefreshing];
        else [self.tableView footerEndRefreshing];
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        if(firstArray && firstArray.count>0){
            if(isReresh) [self.stateData removeAllObjects];
            [self.stateData addObjectsFromArray:firstArray];
            self.stateContentState = 3;
            if(isReresh) [self setupRefresh];//添加下拉
        } else {
            if(isReresh) self.stateContentState = 1;
        }
        [self.tableView reloadData];
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh) self.stateContentState = 2;
        [self stopRefresh];
        [self.tableView reloadData];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh) self.stateContentState = 2;
        [self stopRefresh];
        [self.tableView reloadData];
    }];
}

- (void)materialChargeInCell:(GEObjectValueTableViewCell*)cell indexPath:(NSIndexPath *)indexPath
{
    NSArray *titles = [[NSArray alloc] initWithObjects:@"原材供方偏差分析", @"原材平均价格分析", nil];
    NSArray *icons = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"issueTree"], [UIImage imageNamed:@"hollow_out_star_blue"], nil];
    
    UIView *view1 = [self materialOffsetChargeInCell:cell indexPath:indexPath];
    UIView *view2 = [self materialPriceChargeInCell:cell indexPath:indexPath];
    
    NSArray *views = [[NSArray alloc] initWithObjects:view1, view2, nil];
    YFViewPager *_viewPager = [[YFViewPager alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 285)
                                             titles:titles icons:icons selectedIcons:nil views:views];
    _viewPager.tabSelectedArrowBgColor = ThemeColorBlue;
    _viewPager.tabSelectedTitleColor = ThemeColorBlue;
    _viewPager.tabTitleColor = [UIColor colorWithWhite:0.1 alpha:0.75];
    _viewPager.showBottomLineHeight = 0.5;
    _viewPager.vLineWidth = 0.5;
    [cell.contentView addSubview:_viewPager];
}

#pragma mark - 原材供方偏差

- (UIView*)materialOffsetChargeInCell:(GEObjectValueTableViewCell*)cell indexPath:(NSIndexPath *)indexPath
{
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 265)];
    NSArray *valueArray = [cell.objectValue lastObject];

    BarChartView *_chartView = [[BarChartView alloc] initWithFrame:CGRectMake(0, 10, SuperWidth, 230.0)];
    //_chartView.delegate = self;
//    _chartView.descriptionText = @"";
    _chartView.noDataText = @"暂无数据";
    
    _chartView.chartDescription.enabled = NO;
    _chartView.pinchZoomEnabled = NO;
    _chartView.drawBarShadowEnabled = NO;
    _chartView.drawGridBackgroundEnabled = NO;
    
    ChartLegend *l = _chartView.legend;
    l.horizontalAlignment = ChartLegendHorizontalAlignmentCenter;
    l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
    l.orientation = ChartLegendOrientationHorizontal;
    l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
    
    ChartXAxis *xAxis = _chartView.xAxis;
    xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    xAxis.centerAxisLabelsEnabled = YES;
    xAxis.labelPosition = XAxisLabelPositionBothSided;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.axisMinimum = 0;
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    leftAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    //leftAxis.drawGridLinesEnabled = NO;
    leftAxis.spaceTop = 0.25;
    leftAxis.axisMinimum = 0;
    
    ChartYAxis *rightAxis = _chartView.rightAxis;
    rightAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    rightAxis.drawGridLinesEnabled = NO;
    rightAxis.spaceTop = 0.25;
    rightAxis.axisMinimum = 0;
    
    _chartView.data = [self generateBar:_chartView data:valueArray];
    [contentView addSubview:_chartView];
    
    return contentView;
}

- (BarChartData *)generateBar:(BarChartView*)chartView data:(NSArray*)array
{
    NSMutableArray *xVals = [NSMutableArray array];
    for (int j = 0; j < array.count; j++) {
        NSDictionary *mDictionary = [array objectAtIndex:j];
        NSString *clmc = [mDictionary objectFormatStringForKey:@"材料名称"];
        [xVals addObject:clmc];
    }
    
    CGFloat weight3 = 0;
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals3 = [[NSMutableArray alloc] init];
    for (int i = 0; i < array.count; i++) {
        NSDictionary *mDictionary = [array objectAtIndex:i];
        
        CGFloat weight1 = [mDictionary objectFormatFloatForKey:@"净重" retain:1 except:1];
        [yVals1 addObject:[[BarChartDataEntry alloc] initWithX:i y:weight1]];
        
        CGFloat weight2 = [mDictionary objectFormatFloatForKey:@"供方重量" retain:1 except:1];
        [yVals2 addObject:[[BarChartDataEntry alloc] initWithX:i y:weight2]];
        
        weight3 = weight2 - weight1;
        [yVals3 addObject:[[BarChartDataEntry alloc] initWithX:i y:fabs(weight3)]];
    }
    
    //设置底部显示文字
    chartView.xAxis.granularity = 1.0; // only intervals
    chartView.xAxis.valueFormatter = [[StringAxisValueFormatter alloc] initWithData:xVals];
    
    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yVals1 label:@"运单重量"];
    [set1 setColor:ThemeColorGreen];
    BarChartDataSet *set2 = [[BarChartDataSet alloc] initWithValues:yVals2 label:@"实际重量"];
    [set2 setColor:ThemeColorLigthBlue];
    BarChartDataSet *set3 = [[BarChartDataSet alloc] initWithValues:yVals3 label:@"供方偏差"];
    [set3 setColor:ThemeColorOrange];
    
    //添加符号
    if(weight3 < 0){
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.maximumFractionDigits = 1;
        formatter.negativePrefix = @"-";
        formatter.positivePrefix = @"-";
        [set3 setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:formatter]];
    }
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    [dataSets addObject:set2];
    [dataSets addObject:set3];
    
    NSInteger groupCount = 1;
    float groupSpace = 0.08f;
    float barSpace = 0.03f;
    float barWidth = 0.2f;
    // (0.2 + 0.03) * 3 + 0.08 = 1.00 -> interval per "group"
    
    BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
    
    // specify the width each bar should have
    data.barWidth = barWidth;
    
    // restrict the x-axis range
    chartView.xAxis.axisMinimum = 0;
    
    // groupWidthWithGroupSpace(...) is a helper that calculates the width each group needs based on the provided parameters
    chartView.xAxis.axisMaximum = [data groupWidthWithGroupSpace:groupSpace barSpace: barSpace] * groupCount;
    
    [data groupBarsFromX:0 groupSpace:groupSpace barSpace:barSpace];
    
    return data;
}

#pragma mark - 原材平均价格统计分析

- (UIView*)materialPriceChargeInCell:(GEObjectValueTableViewCell*)cell indexPath:(NSIndexPath *)indexPath
{
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 265)];
    NSArray *arrayObject = cell.objectValue;
    arrayObject = [arrayObject firstObject];
    
    LineChartView *_chartView = [[LineChartView alloc] initWithFrame:CGRectMake(0, 10, SuperWidth, 230.0)];
//    _chartView.descriptionText = @"";
    _chartView.drawBordersEnabled = YES;
    _chartView.noDataText = @"暂无数据";
    
    _chartView.drawGridBackgroundEnabled = NO;
    _chartView.dragEnabled = YES;
    [_chartView setScaleEnabled:YES];
    _chartView.pinchZoomEnabled = NO;
    
    ChartLegend *l = _chartView.legend;

    l.horizontalAlignment = ChartLegendHorizontalAlignmentCenter;
    l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
    l.orientation = ChartLegendOrientationHorizontal;
    l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
    
    ChartXAxis *xAxis = _chartView.xAxis;
    xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    xAxis.drawGridLinesEnabled = NO;
    xAxis.axisMinimum = 0;
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    leftAxis.axisMinimum = 0;
    leftAxis.drawGridLinesEnabled = YES;
    leftAxis.drawZeroLineEnabled = NO;
    leftAxis.granularityEnabled = YES;
    
    ChartYAxis *rightAxis = _chartView.rightAxis;
    rightAxis.axisMinimum = 0;
    
    [self setDataCount:_chartView value:arrayObject];
    [contentView addSubview:_chartView];
    
    return contentView;
}

- (void)setDataCount:(LineChartView *)_chartView value:(NSArray*)arrayValue
{
    if(arrayValue.count <=0 ) return;
    
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < arrayValue.count; i++){
        NSDictionary *mDictionary = [arrayValue objectAtIndex:i];
        [xVals addObject:[mDictionary objectFormatStringForKey:@"开始日期" format:@"%@日" defaults:@"未知"]];
    }
    //设置底部显示文字
    _chartView.xAxis.granularity = 1.0; // only intervals
    _chartView.xAxis.valueFormatter = [[StringAxisValueFormatter alloc] initWithData:xVals];
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    for (int z = 0; z < 1; z++){
        NSMutableArray *values = [[NSMutableArray alloc] init];
        for (int i = 0; i < arrayValue.count; i++) {
            NSDictionary *mDictionary = [arrayValue objectAtIndex:i];
            CGFloat val = [mDictionary objectFormatFloatForKey:@"单价"];
            [values addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
        }
        
        LineChartDataSet *d = [[LineChartDataSet alloc] initWithValues:values label:@"平均单价    图表可手势放大缩小"];
        d.lineWidth = 2.5;
        d.circleRadius = 4.0;
        
        UIColor *color = ChartColorTemplates.vordiplom[0];
        [d setColor:color];
        [d setCircleColor:color];
        [dataSets addObject:d];
    }
    
    LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
    _chartView.data = data;
}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 2) {
        if(self.stateContentState == 0) return 1;
        else if(self.stateContentState == 1) return 1;
        else if(self.stateContentState == 2) return 1;
        else if(self.stateContentState == 3) return self.stateData.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        AutoTextTableViewCell *cell = [[AutoTextTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AutoTextTableViewCell"];
        [cell setDataArray:self.chargeInBaseData];
        return cell;
        
    } else if(indexPath.section == 1){
        //绘制图表数据
        GEProgressBarTableViewCell *progressBarCell = [self.tableView getCellViewInRow:0 inSection:indexPath.section];
        if (progressBarCell == nil) {
            NSString *identifierBar = [NSString stringWithFormat:@"MCGEProgressBarTableViewCell%ld",indexPath.section];
            progressBarCell = [[GEProgressBarTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierBar];
            progressBarCell.selectionStyle = UITableViewCellSelectionStyleNone;
            [self.tableView setCellView:progressBarCell inRow:0 inSection:indexPath.section];
        }
        
        //存在数据绘制图表
        if (progressBarCell.objectValue) {
            NSString *identifier = [NSString stringWithFormat:@"MCChargeInDetailViewController%ld",(long)indexPath.section];
            GEObjectValueTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[GEObjectValueTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.objectValue = progressBarCell.objectValue;
                [self materialChargeInCell:cell indexPath:indexPath];
            }
            return cell;
        }
        
        NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
        [parameter setObject:[self.contractBaseInfo objectFormatStringForKey:@"CLID"] forKey:@"CLID"];
        [parameter setObject:[self.contractBaseInfo objectFormatStringForKey:@"GYSID"] forKey:@"GYSID"];
        [parameter setObject:self.searchStartTime forKey:@"开始日期"];
        [parameter setObject:self.searchEndTime forKey:@"截止日期"];
        [progressBarCell getNetWorkDataTableView:tableView moduleType:@"YCL_JLTJ_PRICE_CHART20160201" parameter:parameter index:indexPath.section];
        return progressBarCell;
        
    } else if (indexPath.section == 2) {
         UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ActivityIndicat"];
         cell.contentView.frame = CGRectMake(0, 0, SuperWidth, 55);
         
         if(self.stateContentState == 0) {//加载进度条
             UIActivityIndicatorView *act=[[UIActivityIndicatorView  alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
             act.center = CGPointMake(SuperWidth/2, 26.5);
             act.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
             act.hidesWhenStopped = YES;
             [act startAnimating];
             [cell.contentView addSubview:act];
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
             return cell;
             
         } else if(self.stateContentState == 1){//显示无数据提示
             UILabel* label = [[UILabel alloc] initWithFrame:cell.contentView.frame];
             label.text = @"没有进料信息";
             label.font = [UIFont fontWithName:@"Arial" size:16];
             label.textColor = [UIColor lightGrayColor];
             label.textAlignment = NSTextAlignmentCenter;
             [cell.contentView addSubview:label];
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
             return cell;
             
         } else if(self.stateContentState == 2){//显示失败提示
             UILabel* label = [[UILabel alloc] initWithFrame:cell.contentView.frame];
             label.text = @"加载数据失败，点击重试";
             label.font = [UIFont fontWithName:@"Arial" size:16];
             label.textColor = [UIColor lightGrayColor];
             label.textAlignment = NSTextAlignmentCenter;
             [cell.contentView addSubview:label];
             return cell;
             
         } else {
             NSString *identifier = [NSString stringWithFormat:@"MCChargeInDetailViewCell"];
             MCChargeInDetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
             if (cell == nil) {
                 cell = [[MCChargeInDetailViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
             }
             
             NSDictionary *mDictionary = [self.stateData objectAtIndex:indexPath.row];
             cell.gbdhLabel.text = [mDictionary objectFormatStringForKey:@"过磅单号" defaults:@"未知"];
             cell.clggLabel.text = [mDictionary objectFormatStringForKey:@"材料名称" defaults:@"未知"];
             cell.gysLabel.text = [mDictionary objectFormatStringForKey:@"供应商" defaults:@"未知"];
             cell.jlsjLabel.text = [mDictionary objectFormatStringForKey:@"出场称重时间" defaults:@"未知"];
//             cell.jgLabel.text = [mDictionary objectFormatFloatStringForKey:@"金额" retain:0 format:@"%@¥"];
             cell.zlLabel.text = [mDictionary objectFormatFloatStringForKey:@"打印净重" retain:2  format:@"%@吨"];
//             cell.djLabel.text = [mDictionary objectFormatFloatStringForKey:@"单价" retain:1 format:@"%@元/吨"];
             return cell;
         }
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 0.5)];
        lineView.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.1];
        return lineView;
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, SuperWidth - 10, 29.5)];
    lable.textColor = [UIColor darkGrayColor];
    lable.font = [UIFont boldSystemFontOfSize:15];
    
     if(section == 0){
         lable.text = @"原材基本信息";
     } else if (section == 1) {
         lable.text = @"原材平均价格走势";
     } else if(section == 2){
        lable.text = @"原材进料单";
    }
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29.5, SuperWidth, 0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
    [view addSubview:lineView];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) return 0.5;
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section != 2) return 0;
    if (self.stateData.count > 0) return 0.5;
    else return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        AutoTextTableViewCell *cell = [[AutoTextTableViewCell alloc] init];
        [cell setDataArray:self.chargeInBaseData];
        return cell.contentView.frame.size.height;
        
    } else if(indexPath.section == 1){
        return 285;
        
    } else if(indexPath.section == 2){
        if(self.stateContentState == 3) return 80;
        else return 55;
    }
    
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 2){
        if(self.stateContentState == 2) {//加载失败执行
            self.stateContentState = 0;
            [self.tableView reloadData];
            [self headerRereshing];
        } else if(self.stateContentState == 3){
            NSDictionary *mDictionary = [self.stateData objectAtIndex:indexPath.row];
            MCChargeInWeighDetailViewController *view = [[MCChargeInWeighDetailViewController alloc] initWithValue:mDictionary];
            [self.navigationController pushViewController:view animated:YES];
        }
    }
}

@end
